package messenger;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Logger;

public class MSNServer {
	private Logger log = Logger.getLogger("MSNServer");
	public String username, password;
	private int command = 0;
	private String message;
	private Socket server;
	private PrintWriter out;
	private Scanner in;
	public String temp;
	private boolean flag ;
	
	public static void main (String[] args) {
		Runtime r = Runtime.getRuntime();
		try {
			Process p = r.exec("telnet zcydsd311");
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
			try {
				Thread.sleep(3000);
				writer.write("nortel");
				writer.flush();
				Thread.sleep(3000);
				writer.write("oam_nortel");
				writer.flush();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			while(reader.ready()) {
				System.out.println(reader.readLine());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		MSNServer server = new MSNServer("zjy@163.net","111111");
//		try {
//			server.login();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	public MSNServer(String username, String password) {
		this.username = username;
		this.password = password;
	}


	public void login() throws Exception {
		log.info("Login");
		server = new Socket("messenger.hotmail.com", 1863);
		server.setTrafficClass(0x10);
		out = new PrintWriter(server.getOutputStream(), true);
		in = new Scanner(new BufferedInputStream(server
				.getInputStream()));

		
		 message = "VER " + command + " <SPAN class=hilite1>MSN</SPAN>10 <SPAN class=hilite1>MSN</SPAN>P9 CVRO ";
		out.println(message);
		command++;
		temp = in.nextLine();
		System.out.println(temp);
		

		
		message = "CVR " + command
				+ " 0x0804 winnt 5.2 i386 <SPAN class=hilite1>MSN</SPAN>MSGR 7.5.0299 MSMSGS " + username
				+ " ";
		out.println(message);
		command++;
		temp = in.nextLine();
		System.out.println(temp);

		
		message = "USR " + command + " TWN I " + username + " ";
		out.println(message);
		command++;
		temp = in.nextLine();
		System.out.println(temp);


		StringTokenizer st = new StringTokenizer(temp);
		st.nextToken();
		st.nextToken();
		st.nextToken();
		String ns = st.nextToken();
		out.close();
		in.close();
		server.close();
		String[] split = ns.split(":");
		server = new Socket(split[0], Integer.parseInt(split[1]));
		server.setTrafficClass(0x10);
		out = new PrintWriter(server.getOutputStream(), true);
		in = new Scanner(new BufferedInputStream(server
				.getInputStream()));
		message = "VER " + command + " <SPAN class=hilite1>MSN</SPAN>10 <SPAN class=hilite1>MSN</SPAN>P9 CVR0";
		command++;
		out.println(message);
		temp = in.nextLine();
		System.out.println(temp);

		
		message = "CVR " + command
				+ " 0x0804 winnt 5.2 i386 <SPAN class=hilite1>MSN</SPAN>MSGR 7.5.0299 MSMSGS " + username
				+ " ";
		out.println(message);
		command++;
		temp = in.nextLine();


		message = "USR " + command + " TWN I " + username + " ";
		out.println(message);
		command++;
	}
}